package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.CreateUserService;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/createUser")
public class CreateUser {

    @Reference
    private CreateUserService createUserService;
    @Reference
    private RoleService roleService;

    @RequestMapping("/add")
    public Result add(String img, @RequestBody User user,Integer[] checkitemIds){

        //编码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        if(!StringUtils.isEmpty(password)){
            String encode = bCryptPasswordEncoder.encode(password);
            user.setPassword(encode);
        }
        try {
            createUserService.add(user,img,checkitemIds);
            return new Result(true, MessageConstant.ADD_CREATEUSER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CREATEUSER_FAIL);
        }
    }

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception{

        PageResult pageResult = createUserService.findPage(queryPageBean);
        return pageResult;
    }

    //回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            User user = createUserService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    //回显照片数据
    @RequestMapping("/findByIdByImg")
    public Result findByIdByImg(Integer id){

        try {
            String img = createUserService.findByIdByImg(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,img);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,String img,Integer[] checkitemIds){
        //编码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        User user1 = createUserService.findById(user.getId());
        String password1 = user1.getPassword();
        if(password1 != null){
            if(!password1.equals(password)){
                String encode = bCryptPasswordEncoder.encode(password);
                user.setPassword(encode);
            }
        }

        try {
            createUserService.edit(user,img,checkitemIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

    //删除
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){

        try {
            createUserService.delete(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<Role> roles = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //回显角色
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkItems = roleService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,checkItems);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
